package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.ReserveOnDayDTO;
import com.sky.dto.TeacherDTO;
import com.sky.dto.TeacherPageQueryDTO;
import com.sky.entity.PeopleTeacher;
import com.sky.entity.Teacher;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.PeopleTeacherMapper;
import com.sky.mapper.TeacherMappper;
import com.sky.result.PageResult;
import com.sky.service.TeacherService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMappper teacherMappper;
    //医师预约
    @Autowired
    private PeopleTeacherMapper peopleTeacherMapper;

    @Override
    public List<Teacher> TeacherList() {
        List<Teacher> teacherList = teacherMappper.teacherList();
        return teacherList;
    }

    /**
     * 医师预约
     * @param teacherDTO
     */
    @Override
    public void reserveTeacher(TeacherDTO teacherDTO) {
        PeopleTeacher peopleTeacher=new PeopleTeacher();
        BeanUtils.copyProperties(teacherDTO,peopleTeacher);
        peopleTeacher.setPeoId(BaseContext.getCurrentId());
        peopleTeacher.setTerId(teacherDTO.getId());
        peopleTeacher.setResTime(teacherDTO.getTerTime());
        Teacher teacher=new Teacher();
        teacher.setTerTime(teacherDTO.getTerTime());
        teacher.setId(teacherDTO.getId());
        teacherMappper.updateById(teacher);
        int check = peopleTeacherMapper.check(peopleTeacher);
        if(check>0){
            System.out.println("已经预约该医师！");
            throw new OrderBusinessException("已预约该医师");
        } else {
            peopleTeacherMapper.insert(peopleTeacher);
        }
    }

    @Override
    public Teacher getOne(Long id) {
        Teacher teacher = teacherMappper.selectById(id);
        return teacher;
    }

    @Override
    public PageResult pageQuery(TeacherPageQueryDTO teacherPageQueryDTO) {
        PageHelper.startPage(teacherPageQueryDTO.getPage(),teacherPageQueryDTO.getPageSize());
        Page<Teacher> page=teacherMappper.pageQuery(teacherPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Teacher> selectAllReserveTeacher() {
        Long perId=BaseContext.getCurrentId();
        List<Teacher> teacherList=teacherMappper.selectReserveTeacherById(perId);
        return teacherList;
    }

    @Override
    public void deleteReserveTeacher(ReserveOnDayDTO reserveOnDayDTO) {
        peopleTeacherMapper.deleteReserveTeacher(reserveOnDayDTO);
    }

    @Override
    public List<Teacher> selectByDay(LocalDate day) {
        List<Teacher> teacherList=teacherMappper.selectByDay(day);
        return teacherList;
    }

    @Override
    public Teacher getOneByday(Long terId, LocalDate day) {
        Teacher teacher=teacherMappper.getOneByDay(terId,day);
        teacher.setTerTime(day);
        return teacher;
    }


}
